package com.rabbit.mq.config;

import com.rabbit.mq.handler.HandlerContext;
import com.rabbit.mq.handler.QueueHandler;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @version V1.0.0
 * @ClassName: {@link MyAckReceiver}
 * @Description: MyAckReceiver
 * @author: Lanzhou
 * @date: 2021/3/18 16:33
 * @Copyright:2020 All rights reserved.
 */
@Component
public class MyAckReceiver implements ChannelAwareMessageListener {

    @Resource
    private HandlerContext handlerContext;

    /**
     * Message里面的内容是这样的: (Body:'{createTime=2021-03-18 16:50:03, messageId=5cde6be1-54ad-4e04-8753-55a7daf0eeb0, messageData=this is message is ack !}' MessageProperties [headers={spring_listener_return_correlation=237b9bb8-4aad-4e35-a467-6ded0272e8cb}, contentType=application/x-java-serialized-object, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=false, receivedExchange=exchangeAck, receivedRoutingKey=queue_exchange_ack, deliveryTag=4, consumerTag=amq.ctag-CEzTPv0MaHbmXeQkQaocLw, consumerQueue=queueAck])
     *
     * @param message
     * @param channel
     * @throws Exception
     */
    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        String queueName = message.getMessageProperties().getConsumerQueue();
        QueueHandler handler = handlerContext.getHandler(queueName);
        handler.handle(message, channel);
    }


}
